#-------------------------------------------------------------------------------
# Name:        Main
# Purpose:
#
# Author:      Pablo
#
# Created:     11/02/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, sys, os, webbrowser, logging
import game, title, config, options
import time as pytime
from ..tools import videotools, versioncheck
from pygame.locals import *

def main():

  logging.info('Initializing #HJ Engine')

#--------------------------- Main Window Inits ---------------------------------


  if sys.platform == 'win32' or sys.platform == 'win64':
    os.environ['SDL_VIDEO_CENTERED'] = '1' #Center screen on windows
    os.environ['SDL_VIDEODRIVER'] = 'windib' #Use better video thing on windows

  fpsClock = pygame.time.Clock()
  fps = 0
  meanfps = []
  lowestfps = 1000000
  highestfps = 0
  averagefps = 0
  lowestfps = 0
  highestfps = 0
  runtime = 0
  time = 0
  screenshot = False
  poll = 0

#-------------------------------------------------------------------------------

  # Pre-init mixer for sounds. I guess some OSes require this, even though we are going to
  # quit the mizer and then reinitialize after the intro video anyways.
  pygame.mixer.pre_init(frequency=22050, size=-16, channels=2, buffer=1024)
  pygame.init()
  pygame.key.set_repeat(1,10) # Keys repeat

  #Initialize game
  g = game.Game()

  pygame.display.set_caption(g.common.caption)
  g.screen.set_colorkey((255,0,255))

  time = 0
  running = True # Controls the main loop
  mouse = [(0,0), 0, 0, 0, 0, 0, 0]

#--------------------- Process Commandline Arguments --------------------------

  nologo = False
  debug = False
  nofps = False
  editor = False
  fpscap = 70
  titlefps = False
  debuglite = False
  balloonmode = False

  if len(sys.argv) > 1:
    g.debug.setArgv(sys.argv[1:])
    for argv in sys.argv:
      if argv == '-nologo':
        nologo = True
      elif argv == '-debug':
        debug = True
        g.debug.setEnabled(True)
      elif argv == '-nofps':
        nofps = True
      elif argv == '-editor':
        editor = True
      elif argv == '-fps':
        titlefps = True
      elif argv == '-debuglite':
        debuglite = True
      elif argv.startswith('-fpscap:'):
        fpscap = float(argv[8:])
        if fpscap > 200:
          fpscap = 200
      elif argv.startswith('-mode:'):
        g.setMode(int(argv[6:]))
      elif argv == '-exception':
        raise RuntimeError('-exception commandline argument used.')
      elif argv == '-99':
        balloonmode = True

      g.debugstates = (debug,debuglite)

#---------------------------- Splash Screen ------------------------------------

  if nologo == False:
    pygame.mouse.set_visible(False)
    splashrunning = True
    splash = title.ImageTitle(g.titleS)

    while splashrunning == True:
      # Keep the window the same scale
      for event in pygame.event.get():
        if event.type == QUIT:
          splashrunning = False
          running = False
        elif event.type == KEYDOWN:
          if event.key == K_ESCAPE:
            splashrunning = False

      temp = splash.draw(0)
      if temp == 0:
        running = False
        splashrunning = False
      elif temp == 1:
        splashrunning = False

      # Update the screen
      titletemp = pygame.Surface(g.size)
      pygame.transform.smoothscale(g.titleS, g.size, titletemp)
      g.screen.blit(titletemp, (0,0))
      pygame.display.flip()
      fpsClock.tick(30)

  # Reset sound mixer and initialize the engine sounds if we are not quitting the game.
  if running == True:
    pygame.mixer.quit()
    pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=g.common.buffersize)
    pygame.display.set_caption(g.common.caption + ': Loading...')
    g.postInit()
    pygame.display.set_caption(g.common.caption)
    # Awww, we get a cute mouse again :D
    pygame.mouse.set_visible(True)


#-------------------------------------------------------------------------------
#------------------------------ Version Check ----------------------------------
#-------------------------------------------------------------------------------

  checkversion= versioncheck.VersionCheck()
  checkversion.check(g.common.name, g.common.version)

#-------------------------------------------------------------------------------
#-------------------------------- MAIN LOOP ------------------------------------
#-------------------------------------------------------------------------------

  while running == True:
    # Reset mouse variable
    mouse = [mouse[0], 0, 0, 0, 0, 0, 0]

    # Main event loop will only handle the mouse. This is due to pygame event being weird
    # when being called twice. So anything that needs the mouse will have the mouse list
    # passed in as an argument. Classes that require keypresses will handle those themselves
    # using pygame.key.get_pressed()[index] and passing g.key.bind to get the user-defined
    # keyboard controls

    for event in pygame.event.get():
      if event.type == QUIT or g.checkQuit() == True:
        pygame.quit()
        sys.exit()
      # Mouse -----
      if event.type == MOUSEMOTION:
        mouse[0] = g.common.videomouse.scale(event.pos, g.size)
      if event.type == MOUSEBUTTONDOWN:
        mouse[event.button] = 1
      if event.type == MOUSEBUTTONUP:
        mouse[event.button] = 2

      if event.type == KEYDOWN:

        if event.key == g.common.key.bind['screenshot']:
          datetime = pytime.strftime('%m-%d-%Y_%H.%M.%S')
          screenshot = True


#-------------------------------------------------------------------------------


    # Scale the preS surface immediately after checking if the window size has changed.
    g.preS = pygame.Surface(g.size)
##    g.screen.fill((255,255,255))



#-------------------------------------------------------------------------------
#----------------------------------- MODES -------------------------------------
#-------------------------------------------------------------------------------

    # Here the engine checks what mode the engine is in, and executes the appropriate code.
    # The lost of modes can be found in game.py


    # ---- Main Menu ----
    if g.mode == 0:

      # Drawing menu surface. This will get moved to bottom of menu section later, I think.
      g.options.firstdraw = True
      menutemp = ''
      if balloonmode:
        menutemp = g.menu.draw(mouse, True)
      else:
        menutemp = g.menu.draw(mouse)
      pygame.transform.smoothscale(menutemp, g.size, g.preS)

      # Debug testing here.
      if debug == True:
        pass

      # Menu buttons.
      if g.menu.getSelected() == (0,0):
        pass
      else:
        state = g.menu.getSelected()
        g.debug.add('Button: ' + str(state[0]) + ' Mode: ' + str(state[1]))
        mode = state[1]
        if mode == 'QUIT':
          g.quit = 1
        elif mode == 'LINK':
          webbrowser.open_new_tab(g.menu.menu_item[state[0] - 1].link)
          g.debug.add('Opening ' + str(g.menu.menu_item[state[0] - 1].link))
        else:
          g.setMode(int(state[1]))

    # ---- Options Menu ----
    if g.mode == 4:

      # reset main menu
      g.menu.reset()

      if g.options.getSelected() == (0,0):
        pass
      else:
        state = g.options.getSelected()
        g.debug.add('Button: ' + str(state[0]) + ' Mode: ' + str(state[1]))
        mode = state[1]
        if mode == 'QUIT':
          g.options.quit()
          g.usePrevMode()
          # Settings don't reset correctly, so we just reinit the options class after
          # reloading g.common.settings with g.options.quit()
          g.options = options.Options(g.optionS, 'options.ini', g.common, g.sound, g.music, g.debug, g.size)
        elif mode == 'SAVE':
          g.options.save()
          g.usePrevMode()
          g.size = (g.common.reswidths[g.common.settings.res_setting], g.size[1])
          g.size = g.common.videoresize.scale(g.common.scale, g.size) # Get new size
          g.preS = pygame.Surface(g.size)
          if g.common.settings.fullscreen == True:
            pygame.display.set_mode(g.size, SRCALPHA|DOUBLEBUF|FULLSCREEN)
          else:
            pygame.display.set_mode(g.size, SRCALPHA|DOUBLEBUF)


      # Draw options menu
      optionstemp = g.options.draw(mouse, g.prevmode)
      pygame.transform.smoothscale(optionstemp, g.size, g.preS)


    # ---- Game Mode ----
    if g.mode == 1:
      g.runGame(int(fpsClock.get_rawtime()))
      g.updatePreS(g.gameS)

    # ---- n=New Game ----
    if g.mode == 2:
      g.menu.reset()
      g.newGame()
      g.setMode(1)

    # ---- Load Game ----
    if g.mode == 3:
      g.menu.reset()
      g.loadSave('1')
      g.setMode(1)

    # ---- Pause Screen ----
    if g.mode == 5:
      surf, temp = g.pause.draw(g.gameS, g.world, mouse)
      # Return to game if they hit the unpuase button
      if temp == 1:
        g.setMode(temp)
      g.updatePreS(surf)
      if g.mode != 5:
        # replace this later with having the game mode recheck if cursor should be drawn, etc
        pygame.mouse.set_visible(False)
        g.pause.count = 0

      # Buttons
      if g.pause.getSelected() == (0,0):
        pass
      else:
        state = g.pause.getSelected()
        g.debug.add('Button: ' + str(state[0]) + ' Mode: ' + str(state[1]))
        mode = state[1]
        if mode == 'QUIT':
          g.quit = 1
        elif mode == 'LINK':
          webbrowser.open_new_tab(g.pause.menu_item[state[0] - 1].link)
          g.debug.add('Opening ' + str(g.pause.menu_item[state[0] - 1].link))
        elif mode == 'SAVE':
          g.saveWorld('1')
        else:
          g.setMode(int(state[1]))

    # Returns engine to previously used mode
    if g.mode == 8:
      g.usePrevMode()


    # ---- MINIGAME LOL ----
    if g.mode == 9:
      surf = g.minigame.draw(g.gameS, mouse)
      g.updatePreS(surf)

      # Buttons
      key = pygame.key.get_pressed()

      if key[pygame.K_F2]:
        g.mode = 1





#-------------------------------- Debug Stuff ----------------------------------

    fps = int(fpsClock.get_fps())
    if debug == True:
      time = int(fpsClock.get_time())
      runtime = int(pygame.time.get_ticks() / 1000)
      minutes = runtime / 60
      seconds = runtime - (minutes * 60)
      if seconds < 10:
        seconds = '0' + str(seconds)
      runtime = str(minutes) + ':' + str(seconds)
      if fps != 0 and fps < lowestfps:
        lowestfps = fps
      if fps > highestfps:
        highestfps = fps

      meanfps.append(fps)
      if len(meanfps) >= 100:
        meanfps.pop(0)
      temp = 0
      for i in meanfps:
        temp += i
      averagefps = temp / len(meanfps)

      # Update debug
      g.debug.resetWidth(g.preS.get_width())

#--------------------- Update Screen and end of loop ---------------------------

    # Draw the final screen
    if g.mode != 1232:
      g.debug.draw(g.preS, fps, averagefps, lowestfps, highestfps, runtime, time, g.mode, g.modes)
      g.screen.blit(g.preS, (0,0))
      pygame.display.flip()
    else:
    # Deprecated Dirty Rect processing
      continue
##      temprect = g.debug.draw(g.screen, fps, averagefps, lowestfps, highestfps, runtime, time)
##      if g.gamecommon.update == True:
##        pygame.display.flip()
##        g.gamecommon.update = False
##      else:
##        g.gamecommon.dirtyrects = g.gamecommon.worldToScreenRects(g.gamecommon.dirtyrects, g.screen.get_size(), g.gameS.get_size())
##        g.gamecommon.dirtyrects += g.gamecommon.scaleDebugRects(temprect, g.screen.get_size(), g.debugS.get_size())
##        pygame.display.update(g.gamecommon.dirtyrects)




#---------------------------- Clean up screen ----------------------------------

    # Take a screenshot AWWWYEAH
    if screenshot == True:
      pygame.image.save(g.screen, os.path.join('Screenshots/', datetime + '.png'))
      g.debug.add('Screenshot saved')
      screenshot = False

    # Draw fps in titlebar if enabled:
    if titlefps == True:
      pygame.display.set_caption(g.common.caption + ' | FPS: ' + str(fps))

    # Debuglite mode
    if debuglite == True:
      pygame.display.set_caption(g.common.caption + ' | FPS: ' + str(fps) + ' || ' + g.debug.argv)


    # FPS handling
    if nofps == True:
      fpsClock.tick(200)
    else:
      fpsClock.tick(fpscap)
    poll = 0

  # So Pyscripter can quit
  pygame.quit()

if __name__ == '__main__':
  main()
